package com.kurt.controller;

import com.kurt.annotation.Log;
import com.kurt.common.PageResult;
import com.kurt.common.Result;
import com.kurt.dto.create.VehicleDriverCreateRequest;
import com.kurt.dto.create.VehicleDriverDTO;
import com.kurt.dto.query.VehicleDriverQueryRequest;
import com.kurt.service.VehicleDriverService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆驾驶员关联控制器
 * 处理车辆和驾驶员绑定相关的HTTP请求
 */
@Slf4j
@RestController
@RequestMapping("/vehicle-drivers")
@RequiredArgsConstructor
public class VehicleDriverController extends BaseController {

    private final VehicleDriverService vehicleDriverService;

    /**
     * 绑定车辆和驾驶员
     *
     * @param createRequest 绑定请求
     * @return 绑定结果
     */
    @PostMapping("/bind")
    @Log(title = "车辆驾驶员绑定", businessType = 1, operatorType = 1)
    public Result<VehicleDriverDTO> bindVehicleDriver(@RequestBody VehicleDriverCreateRequest createRequest) {
        try {
            createRequest.setCreateUserId(getCurrentUserId());
            log.info("绑定车辆和驾驶员请求: vehicleId={}, driverId={}", createRequest.getVehicleId(), createRequest.getDriverId());
            VehicleDriverDTO result = vehicleDriverService.bindVehicleDriver(createRequest);
            return Result.success("绑定成功", result);
        } catch (Exception e) {
            log.error("绑定车辆和驾驶员失败: {}", e.getMessage(), e);
            return Result.error("绑定失败: " + e.getMessage());
        }
    }

    /**
     * 解绑车辆和驾驶员
     *
     * @param vehicleId 车辆ID
     * @param driverId  驾驶员ID
     * @return 解绑结果
     */
    @DeleteMapping("/unbind/{vehicleId}/{driverId}")
    @Log(title = "车辆驾驶员解绑", businessType = 3, operatorType = 1)
    public Result<Boolean> unbindVehicleDriver(@PathVariable("vehicleId") Long vehicleId,
                                               @PathVariable("driverId") Long driverId) {
        try {
            log.info("解绑车辆和驾驶员请求: vehicleId={}, driverId={}", vehicleId, driverId);
            boolean success = vehicleDriverService.unbindVehicleDriver(vehicleId, driverId);
            return Result.success("解绑成功", success);
        } catch (Exception e) {
            log.error("解绑车辆和驾驶员失败: {}", e.getMessage(), e);
            return Result.error("解绑失败: " + e.getMessage());
        }
    }

    /**
     * 批量绑定车辆和驾驶员
     *
     * @param vehicleId 车辆ID
     * @param driverIds 驾驶员ID列表
     * @return 绑定结果
     */
    @PostMapping("/bind/{vehicleId}")
    public Result<Boolean> bindVehicleDrivers(@PathVariable("vehicleId") Long vehicleId,
                                              @RequestBody List<Long> driverIds) {
        try {
            log.info("批量绑定车辆和驾驶员请求: vehicleId={}, driverIds={}", vehicleId, driverIds);
            boolean success = vehicleDriverService.bindVehicleDrivers(vehicleId, driverIds, getCurrentUserId());
            return Result.success("批量绑定成功", success);
        } catch (Exception e) {
            log.error("批量绑定车辆和驾驶员失败: {}", e.getMessage(), e);
            return Result.error("批量绑定失败: " + e.getMessage());
        }
    }

    /**
     * 批量解绑车辆和驾驶员
     *
     * @param vehicleId 车辆ID
     * @param driverIds 驾驶员ID列表
     * @return 解绑结果
     */
    @DeleteMapping("/unbind/{vehicleId}")
    public Result<Boolean> unbindVehicleDrivers(@PathVariable("vehicleId") Long vehicleId,
                                                @RequestBody List<Long> driverIds) {
        try {
            log.info("批量解绑车辆和驾驶员请求: vehicleId={}, driverIds={}", vehicleId, driverIds);
            boolean success = vehicleDriverService.unbindVehicleDrivers(vehicleId, driverIds);
            return Result.success("批量解绑成功", success);
        } catch (Exception e) {
            log.error("批量解绑车辆和驾驶员失败: {}", e.getMessage(), e);
            return Result.error("批量解绑失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询车辆驾驶员关联
     *
     * @param queryRequest 查询请求
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<PageResult<VehicleDriverDTO>> getVehicleDriverPage(VehicleDriverQueryRequest queryRequest) {
        try {
            queryRequest.setLoginUserId(getCurrentUserId());
            log.info("分页查询车辆驾驶员关联请求: {}", queryRequest);
            Page<VehicleDriverDTO> vehicleDriverPage = vehicleDriverService.getVehicleDriverPage(queryRequest);

            // 转换为PageResult
            PageResult<VehicleDriverDTO> pageResult = PageResult.of(
                    queryRequest.getPageNum(),
                    queryRequest.getPageSize(),
                    vehicleDriverPage.getTotalElements(),
                    vehicleDriverPage.getContent()
            );

            return Result.success("分页查询成功", pageResult);
        } catch (Exception e) {
            log.error("分页查询车辆驾驶员关联失败: {}", e.getMessage(), e);
            return Result.error("分页查询失败: " + e.getMessage());
        }
    }

    /**
     * 查询车辆驾驶员关联列表
     *
     * @param queryRequest 查询请求
     * @return 关联列表
     */
    @GetMapping("/list")
    public Result<List<VehicleDriverDTO>> getVehicleDriverList(VehicleDriverQueryRequest queryRequest) {
        try {
            queryRequest.setLoginUserId(getCurrentUserId());
            log.info("查询车辆驾驶员关联列表请求: {}", queryRequest);
            List<VehicleDriverDTO> vehicleDrivers = vehicleDriverService.getVehicleDriverList(queryRequest);
            return Result.success("查询成功", vehicleDrivers);
        } catch (Exception e) {
            log.error("查询车辆驾驶员关联列表失败: {}", e.getMessage(), e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 根据车辆ID获取绑定的驾驶员列表
     *
     * @param vehicleId 车辆ID
     * @return 驾驶员列表
     */
    @GetMapping("/vehicle/{vehicleId}/drivers")
    public Result<List<VehicleDriverDTO>> getDriversByVehicleId(@PathVariable("vehicleId") Long vehicleId) {
        try {
            log.info("根据车辆ID获取驾驶员列表请求: {}", vehicleId);
            List<VehicleDriverDTO> drivers = vehicleDriverService.getDriversByVehicleId(vehicleId);
            return Result.success("获取驾驶员列表成功", drivers);
        } catch (Exception e) {
            log.error("根据车辆ID获取驾驶员列表失败: {}", e.getMessage(), e);
            return Result.error("获取驾驶员列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据驾驶员ID获取绑定的车辆列表
     *
     * @param driverId 驾驶员ID
     * @return 车辆列表
     */
    @GetMapping("/driver/{driverId}/vehicles")
    public Result<List<VehicleDriverDTO>> getVehiclesByDriverId(@PathVariable("driverId") Long driverId) {
        try {
            log.info("根据驾驶员ID获取车辆列表请求: {}", driverId);
            List<VehicleDriverDTO> vehicles = vehicleDriverService.getVehiclesByDriverId(driverId);
            return Result.success("获取车辆列表成功", vehicles);
        } catch (Exception e) {
            log.error("根据驾驶员ID获取车辆列表失败: {}", e.getMessage(), e);
            return Result.error("获取车辆列表失败: " + e.getMessage());
        }
    }

    /**
     * 检查车辆和驾驶员是否已绑定
     *
     * @param vehicleId 车辆ID
     * @param driverId  驾驶员ID
     * @return 是否已绑定
     */
    @GetMapping("/check/{vehicleId}/{driverId}")
    public Result<Boolean> isVehicleDriverBound(@PathVariable("vehicleId") Long vehicleId,
                                                @PathVariable("driverId") Long driverId) {
        try {
            log.info("检查车辆和驾驶员绑定状态请求: vehicleId={}, driverId={}", vehicleId, driverId);
            boolean isBound = vehicleDriverService.isVehicleDriverBound(vehicleId, driverId);
            return Result.success("检查成功", isBound);
        } catch (Exception e) {
            log.error("检查车辆和驾驶员绑定状态失败: {}", e.getMessage(), e);
            return Result.error("检查失败: " + e.getMessage());
        }
    }
}
